package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.CategoryConstants;
import com.powernode.domain.Category;
import com.powernode.exception.BusinessException;
import com.powernode.mapper.CategoryMapper;
import com.powernode.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@CacheConfig(cacheNames = "com.powernode.service.impl.CategoryServiceImpl")
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /*
     * 查询所有的分类列表
     * @author RenBoQing
     * @date 2024/7/27 11:43
     * @return java.util.List<com.powernode.domain.Category>
     */
    @Override
    @Cacheable(key = CategoryConstants.ALL_CATEGORY)
    public List<Category> queryAllCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>().orderByDesc(Category::getSeq, Category::getCreateTime));
    }

    /*
     *查询一级分类列表
     * @author RenBoQing
     * @date 2024/7/27 13:45
     * @return java.util.List<com.powernode.domain.Category>
     */
    @Cacheable(key = CategoryConstants.FIRST_CATEGORY_LIST_KEY)
    @Override
    public List<Category> queryFirstCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0).eq(Category::getStatus, 1).orderByDesc(Category::getSeq, Category::getCreateTime));
    }

    /**
     * evict 组合注解清除
     * 新增商品类目
     * @param category
     * @return boolean
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = CategoryConstants.ALL_CATEGORY),
            @CacheEvict(key = CategoryConstants.FIRST_CATEGORY_LIST_KEY)
    })
    public boolean saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 修改商品类目
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = CategoryConstants.ALL_CATEGORY),
            @CacheEvict(key = CategoryConstants.FIRST_CATEGORY_LIST_KEY)
    })
    public boolean updateCategory(Category category) {
        // 修改后的商品的pid 父节点
        Long parentId = category.getParentId();
        // 查询当前分类的详情
        Category beforeCategory = categoryMapper.selectById(category.getCategoryId());
        // 获取当前分类之前的pid 父节点  beforeParentId 为0 表示为1级 如果beforeParentId 不为零表示为2级
        Long beforeParentId = beforeCategory.getParentId();
        //判断商品分类的详情
        //1->2 之前的pid 为0 修改后 pid 不为零
        if (beforeParentId == 0 && null != beforeParentId && parentId != 0) {
            // 查询当前分类包含的子类 如果当前分类包含子类 则不能修改
            List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getCategoryId, category.getCategoryId()));
            if (categoryList.size() != 0&& CollectionUtils.isNotEmpty(categoryList)) {
                throw new BusinessException("当前分类包含子类，不能修改");
            }
        }

        // 2->1 之前的pid不为0 而且 当前的pid 为null
        if (beforeParentId != 0 && parentId == null) {
            // 查询当前分类包含的子类 如果当前分类包含子类 则不能修改
            category.setParentId(null);
        }
        return categoryMapper.updateById(category) > 0;
    }

    /**
     * 删除商品类目
     * 如果一级类目下有子类目 则不能删除
     * @param categoryId
     * @return
     */
    @Override
    public boolean removeCategoryById(Long categoryId) {
        // 判断 当前节点是否有子节点
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getParentId, categoryId));
        if (CollectionUtils.isNotEmpty(categoryList) && categoryList.size() != 0) {
            throw new BusinessException("当前分类包含子类，不能删除");
        }
        // 删除对应的子类目
        return categoryMapper.deleteById(categoryId) > 0;
    }
}